package model.potentialField;

import java.awt.Point;
import java.util.Vector;

abstract class Layer {
	protected Vector<Point> elements;
	protected int[][] potentialField;
	protected int width;
	protected int height;
	
	public Layer(int width, int height){
		this.width = width;
		this.height = height;
		elements = new Vector<Point>();
		potentialField = new int[height][width];
	}
	
	private void validateElements(){
		for(Point e : elements){
			if(!isValid(e))
				removeElement(e);
		}
	}
	
	private boolean isValid(int x, int y){
		return (x>=0 && x<height && y>=0 && y<width);
	}

	private boolean isValid(Point p){
		return(isValid(p.x,p.y));
	}
	
	protected void addElement(int x, int y){
		addElement(new Point(x,y));
	}
	
	protected void addElement(Point p){
		if(isValid(p)){
			this.elements.addElement(p);
		}
	}

	protected void addElements(Vector<Point> points){
		for(Point p : points)
			addElement(p);
	}
	
	protected void removeElement(int index){
		this.elements.remove(index);
	}
	
	protected void removeElement(Point p){
		this.elements.remove(p);
	}
	
	protected void removeElement(int x, int y){
		removeElement(new Point(x,y));
	}
	
	protected void removeElements(Vector<Point> points){
		for(Point p : points)
			removeElement(p);
	}
	
	protected void clear(int[][] pf){
		for(int i = 0; i < this.width; i++)
			for(int j = 0; j < this.height; j++)
				pf[i][j] = 0;
	}
	
	abstract void calculatePf();

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
		validateElements();
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
		validateElements();
	}
	
	protected void print(){
		int i,j;
		for(i=0;i<this.width;i++){
			for(j=0;j<this.height;j++){
				System.out.printf("%02d ",this.potentialField[i][j]);
			}
			System.out.printf("\n");
		}
	}

}
